// pages/mission_company/index.js
var router = require('../../utils/router/index.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    donationtext:'点击选择物资类别',
    donationlist:['测试物资1','测试物资2','测试物资源3'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    const data = router.extract(options);
    if(data != null){
      that.setData({
        data: data
      })
    }
    console.log(data)

    const s = new Set();
    that.setData({
      donations:s
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 
   */
  bind_hospital_info: function(){
    router.push({ 
      name: 'm_institution_index',
      data: {
        ads: ''
        } 
      })
  },
  
  /**
   * 物资类别选择
   */
  binddonationChange: function(e){
    var that = this
    this.setData({
      index: e.detail.value,
      donationtext:'',
      goods_name:that.data.donationlist[e.detail.value]
    })
  },

  /**
   * 新增物资
   */
  add_new_donation: function(){
    var that = this
    var s= that.data.donations
    if(that.data.goods_name == null || 
      that.data.standard == null ||
      that.data.amount == null ||
      that.data.supplier == null){
        return
    }
    s.add({
      "goods_name": that.data.goods_name,
      "standard": that.data.standard,
      "stock": that.data.amount,
      "daily_usage": that.data.supplier
    })

    console.log(s)
    
    that.setData({
      donations:s
    })
  },

  /**
   * 质量标准
   */
  bindstandInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      standard: e.detail.value
    })
  },

  bindnumsInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      amount: e.detail.value
    })
  },
  bindcInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      supplier: e.detail.value
    })
  },

  /**
   * 保存
   */
  save_add_donation: function(){
      var that = this
      wx.showNavigationBarLoading();
      console.log()
      wx.showLoading({
        title: '加载中',
      })
      wx.cloud.callFunction({
        // 云函数名称
        name: 'hospital',
        // 传给云函数的参数
        data:{
          "action": "operation_hospital_info",
          "_id":that.data.data._id,
          "hospital_name": that.data.data.hospital_name,
          "hospital_url": that.data.data.hospital_url,
          "hospital_phone": that.data.data.hospital_phone,
          "hospital_mobile": that.data.data.hospital_mobile,
          "hospital_address": that.data.data.hospital_address,
          "hospital_public": that.data.data.hospital_public,
          "hospital_logo": that.data.data.hospital_logo,
          "hospital_content": that.data.data.hospital_content,
          "hospital_need": Array.from(that.data.donations)
        }
        
      })
      .then(res => {
        console.log(res.result) // 3
        if(res.result.errMsg == "collection.update:ok"){
          wx.switchTab({
            url: '../broadcastlist/index',
          })
        }
      })
      .catch(console.error);
      wx.hideNavigationBarLoading();
      wx.hideLoading()
    }

})